﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKExcelFormTool.Kits
{
	internal class InsertDataToGroup
	{
		public static void WirteDataToGroup(Autodesk.Revit.DB.Document doc, Element ele, string id, string doorAndWinInfo, XYZ pt, List<ElementId> lstOtherIds)
		{
			Guid guid = new Guid(InsertDataToGroup.m_strGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName(InsertDataToGroup.m_strDoorWinSchemaName);
				schemaBuilder.AddSimpleField(InsertDataToGroup.m_strDoorWinIdFileName, typeof(string));
				schemaBuilder.AddSimpleField(InsertDataToGroup.m_strDoorWinInfoFileNmae, typeof(string));
				schemaBuilder.AddSimpleField(InsertDataToGroup.m_strDoorWinInsertPt, typeof(XYZ)).SetUnitType(0);
				schemaBuilder.AddSimpleField(InsertDataToGroup.m_strIsDoorWinGroup, typeof(bool));
				schemaBuilder.AddArrayField(InsertDataToGroup.m_strDoorWinOtherEleId, typeof(ElementId));
				schema = schemaBuilder.Finish();
			}
			else
			{
				try
				{
					Entity entity = ele.GetEntity(schema);
					if (entity != null && entity.Schema != null)
					{
						if (!entity.Get<string>(InsertDataToGroup.m_strDoorWinInfoFileNmae).Equals(doorAndWinInfo))
						{
							Field field = schema.GetField(InsertDataToGroup.m_strDoorWinInfoFileNmae);
							entity.Set<string>(field, doorAndWinInfo);
							ele.SetEntity(entity);
						}
					}
					else
					{
						Entity entity2 = new Entity(schema);
						Field field2 = schema.GetField(InsertDataToGroup.m_strDoorWinIdFileName);
						entity2.Set<string>(field2, id);
						Field field3 = schema.GetField(InsertDataToGroup.m_strDoorWinInfoFileNmae);
						entity2.Set<string>(field3, doorAndWinInfo);
						Field field4 = schema.GetField(InsertDataToGroup.m_strDoorWinInsertPt);
                        entity2.Set<XYZ>(field4, pt, (DisplayUnitType)6);
						Field field5 = schema.GetField(InsertDataToGroup.m_strDoorWinOtherEleId);
						entity2.Set<IList<ElementId>>(field5, lstOtherIds);
						Field field6 = schema.GetField(InsertDataToGroup.m_strIsDoorWinGroup);
						entity2.Set<bool>(field6, true);
						ele.SetEntity(entity2);
					}
					return;
				}
				catch (Exception)
				{
					return;
				}
			}
			Entity entity3 = new Entity(schema);
			Field field7 = schema.GetField(InsertDataToGroup.m_strDoorWinIdFileName);
			entity3.Set<string>(field7, id);
			Field field8 = schema.GetField(InsertDataToGroup.m_strDoorWinInfoFileNmae);
			entity3.Set<string>(field8, doorAndWinInfo);
			Field field9 = schema.GetField(InsertDataToGroup.m_strDoorWinInsertPt);
            entity3.Set<XYZ>(field9, pt, (DisplayUnitType)6);
			Field field10 = schema.GetField(InsertDataToGroup.m_strDoorWinOtherEleId);
			entity3.Set<IList<ElementId>>(field10, lstOtherIds);
			Field field11 = schema.GetField(InsertDataToGroup.m_strIsDoorWinGroup);
			entity3.Set<bool>(field11, true);
			ele.SetEntity(entity3);
		}

		public static object ReadInfoFormGroup(Autodesk.Revit.DB.Document doc, Element ele, string fileName)
		{
			Schema schema = Schema.Lookup(new Guid(InsertDataToGroup.m_strGuid));
			if (schema == null)
			{
				return null;
			}
			Entity entity = ele.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				return null;
			}
			if (fileName.Equals("IsDoorWinGroup"))
			{
				return entity.Get<bool>(fileName);
			}
			if (fileName.Equals("DoorWindowId") || fileName.Equals("DoorWindowInfo"))
			{
				return entity.Get<string>(fileName);
			}
			if (fileName.Equals("DoorWinInsertPt"))
			{
                return entity.Get<XYZ>(fileName, (DisplayUnitType)6);
			}
			if (fileName.Equals("OtherEleId"))
			{
				return entity.Get<IList<ElementId>>(fileName) as List<ElementId>;
			}
			return null;
		}

		private static string m_strGuid = "51099220-65D2-42EB-92C6-471A69F82690";

		private static string m_strDoorWinSchemaName = "DoorWindowSchema";

		private static string m_strDoorWinIdFileName = "DoorWindowId";

		private static string m_strDoorWinInfoFileNmae = "DoorWindowInfo";

		private static string m_strIsDoorWinGroup = "IsDoorWinGroup";

		private static string m_strDoorWinInsertPt = "DoorWinInsertPt";

		private static string m_strDoorWinOtherEleId = "OtherEleId";
	}
}
